package com.eachonline.smart.web.comment;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eachonline.core.JsonResult;
import com.eachonline.core.Pagination;
import com.eachonline.smart.mapper.SmartSupportRelateMapper;
import com.eachonline.smart.model.SmartComment;
import com.eachonline.smart.model.SmartCommentExample;
import com.eachonline.smart.model.SmartCourse;
import com.eachonline.smart.model.SmartCourseExample;
import com.eachonline.smart.model.SmartSupportRelate;
import com.eachonline.smart.model.SmartSupportRelateExample;
import com.eachonline.smart.model.SmartUser;
import com.eachonline.smart.service.CourseService;
import com.eachonline.smart.service.SmartCommentService;
import com.eachonline.smart.service.SmartUserService;
import com.eachonline.smart.web.common.CommonUtil;
import com.eachonline.util.StringUtil;
import com.eachonline.util.UUIDUtil;

@Controller
@RequestMapping("/comment")
public class CommentController {
	
	
	@Autowired
	public SmartCommentService commentService;
	
	
	@Autowired
	public SmartUserService userService;
	
	
	@Autowired
	public CourseService courseService;

	@Resource
	private SmartSupportRelateMapper supportRelateMapper;
	/**
	 * 课程点赞
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/supportComment",method = RequestMethod.POST)
	public JsonResult supportComment(HttpServletRequest request,String commentId,String userId,int su,HttpServletResponse  response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");

		if(su==1)
		{
			  SmartSupportRelateExample  sre=new SmartSupportRelateExample();
			  sre.createCriteria().andBusinessIdEqualTo(commentId).andUserIdEqualTo(userId);
			  List<SmartSupportRelate>  list= supportRelateMapper.selectByExample(sre);
			  if(!list.isEmpty())
			  {
				  return new JsonResult(false, "该评论已经点赞过，不能重复点赞");
			  }
			
            SmartComment comment=commentService.selectByPrimaryKey(commentId);
			
			if(comment==null)
			{
				return new JsonResult(false, "该评论不存在");
			}
			int supportNum=comment.getSupportNum()+su;
			
			if(supportNum<=0)
			{
				supportNum=0;
			}
			comment.setSupportNum(supportNum);	
			commentService.updateByPrimaryKeySelective(comment);
			
			SmartSupportRelate support=new SmartSupportRelate();
			
			support.setSid(UUIDUtil.getUUID());
			support.setUserId(userId);
			support.setBusinessId(commentId);
			supportRelateMapper.insert(support);
			return new JsonResult(true, "成功");
		}
		
		if(su==-1)
		{

			SmartComment comment=commentService.selectByPrimaryKey(commentId);
			
			if(comment==null)
			{
				return new JsonResult(false, "该课程不存在");
			}
			int supportNum=comment.getSupportNum()+su;
			
			if(supportNum<=0)
			{
				supportNum=0;
			}
			comment.setSupportNum(supportNum);	
			commentService.updateByPrimaryKeySelective(comment);
			SmartSupportRelateExample  sre=new SmartSupportRelateExample();
			 sre.createCriteria().andBusinessIdEqualTo(commentId).andUserIdEqualTo(userId);
			supportRelateMapper.deleteByExample(sre);
			
		}
		
		return new JsonResult(false, "参数错误");
	}
	
	
	

	/**
	 * 课程和文章评论
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/save",method = RequestMethod.POST)
	public JsonResult saveComment(HttpServletRequest request, SmartComment  comment){
		
		SmartUser  user=CommonUtil.getCurrentUser(request);
		
		if(StringUtil.isEmpty(comment.getUserComment()))
		{
			return  new JsonResult(false, "评论内容不能为空");
		}
		SmartCourseExample  sce=new SmartCourseExample();
		sce.createCriteria().andSidEqualTo(comment.getBusinessId()).andUserSidEqualTo(user.getSid());
		List<SmartCourse> list=courseService.selectByExample(sce);
		if(list.isEmpty())
		{
			comment.setCommentType("1");

		}else
		{
			comment.setCommentType("0");
		}
		String sid=UUIDUtil.getUUID();
		comment.setUserName(user.getUserName());
		comment.setUserId(user.getSid());
		comment.setCreateTime(new Date());
		comment.setSupportNum(0);
		comment.setSid(sid);
		int  row =commentService.insert(comment);
		if(row>0)
		{
			return   new JsonResult(true, "成功");
		}else{
			return   new JsonResult(true, "请稍后再试");
		}
	}
	
	
	

	/**
	 * 课程和文章评论
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/reply",method = RequestMethod.POST)
	public JsonResult replyComment(HttpServletRequest request, SmartComment  comment){
		
		SmartUser  user=CommonUtil.getCurrentUser(request);
		
		if(StringUtil.isEmpty(comment.getUserComment()))
		{
			return  new JsonResult(false, "评论内容不能为空");
		}
		
		SmartCourseExample  sce=new SmartCourseExample();
		sce.createCriteria().andSidEqualTo(comment.getBusinessId()).andUserSidEqualTo(user.getSid());
		List<SmartCourse> list=courseService.selectByExample(sce);
		if(list.isEmpty())
		{
			comment.setCommentType("1");

		}else
		{
			comment.setCommentType("0");
		}
		String sid=UUIDUtil.getUUID();
		comment.setUserName(user.getUserName());
		comment.setUserId(user.getSid());
		comment.setCreateTime(new Date());
		comment.setSupportNum(0);
		comment.setSid(sid);
		int  row =commentService.insert(comment);
		if(row>0)
		{
			return   new JsonResult(true, "成功");
		}else{
			return   new JsonResult(true, "请稍后再试");
		}
	}
	
	
	
	/**
	 * 删除课程和文章评论
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	public JsonResult saveComment(HttpServletRequest request, String sid){
		int row=commentService.deleteByPrimaryKey(sid);
		if(row>0)
		{
			return   new JsonResult(true, "成功");
		}else
		{
			return   new JsonResult(true, "请稍后再试");
		}
	
	}
	
	/**
	 * 删除课程和文章评论
	 * @param start
	 * @param pageSize
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/queryBusinessComment",method = RequestMethod.POST)
	public JsonResult queryBusinessComment(HttpServletRequest request,String classId,String businessId){
		
		int start=1;
		if(StringUtil.isNotEmpty(request.getParameter("start")))
		{
		  start=Integer.valueOf(request.getParameter("start").toString());
		}
		
		int pageSize=8;
		if(StringUtil.isNotEmpty(request.getParameter("pageSize")))
		{
			pageSize=Integer.valueOf(request.getParameter("pageSize").toString());
		}
		int begin =pageSize*(start-1);
		
		SmartCommentExample commentExa=new SmartCommentExample();
		commentExa.createCriteria().andClassIdEqualTo(classId).andBusinessIdEqualTo(businessId);
		
		Pagination<SmartComment> pageComment=commentService.queryByPage(commentExa, begin, pageSize," create_time desc");
		List<SmartComment>  commentList=pageComment.getDataList();
		if(!commentList.isEmpty())
		{
			for(SmartComment comment:commentList)
			{
				SmartUser user=userService.selectByPrimaryKey(comment.getUserId());
				comment.setUserHeadUrl(user.getUserHeadPicUrl());
				
				SmartCommentExample replyExa=new SmartCommentExample();
				replyExa.createCriteria().andClassIdEqualTo(classId).andBusinessIdEqualTo(businessId).andParentIdEqualTo(comment.getSid());
				Pagination<SmartComment> pageReply=commentService.queryByPage(replyExa, 0, 20," create_time desc");
				List<SmartComment>  replyList=pageReply.getDataList();
				comment.setReplyList(replyList);
			}
			pageComment.setDataList(commentList);
		}
		
	     return new JsonResult(true, pageComment);
	}
	
}
